package com.haixiaoke.flat.system.controller;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.weChat.domain.WeChatLoginParam;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.system.domain.LoginBody;
import com.haixiaoke.saas.user.domain.UserConsumer;
import com.haixiaoke.saas.user.service.IUserConsumerService;
import com.haixiaoke.token.FrontTokenComponent;
import com.haixiaoke.token.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;


/**
 * 登录
 *
 * @author panda
 */
@RestController
@RequestMapping("/front/flat/system/login")
public class FrontLoginController extends BaseController {


    @Autowired
    private FrontTokenComponent frontTokenComponent;
    @Autowired
    private IUserConsumerService userConsumerService;


    /**
     * 微信一键登录
     */
    @PostMapping("/weChat")
    public AjaxResult weChatLogin(@RequestBody WeChatLoginParam loginParam) {
        UserConsumer user = userConsumerService.weChatSelectUserByPhone(loginParam);
        String token = frontTokenComponent.createToken(user);
        // 返回 token 和 用户
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("token", token);
        map.put("userInfo", user);
        return AjaxResult.success("登录成功!", map);
    }

    /**
     * 手机验证码登录
     */
    @PostMapping("/captcha")
    public AjaxResult captchaLogin(@RequestBody LoginBody loginBody) {
        UserConsumer user = userConsumerService.selectUserByCaptcha(loginBody);
        String token = frontTokenComponent.createToken(user);
        // 返回 token 和 用户
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("token", token);
        map.put("userInfo", user);
        return AjaxResult.success("登录成功!", map);
    }

    /**
     * 修改用户
     */
    @PutMapping("/editUser")
    @AuthCheck
    public AjaxResult edit(@RequestBody UserConsumer user) {
        int rows = userConsumerService.updateUserConsumer(user);
        if (rows > 0) {
            LoginUser loginUser = frontTokenComponent.getLoginUser();
            UserConsumer sqlUser = loginUser.getUser();
            sqlUser.setUserName(StrUtil.isNotEmpty(user.getUserName()) ? user.getUserName() : sqlUser.getUserName());
            sqlUser.setUserPhone(StrUtil.isNotEmpty(user.getUserPhone()) ? user.getUserPhone() : sqlUser.getUserPhone());
            sqlUser.setProfilePhoto(StrUtil.isNotEmpty(user.getProfilePhoto()) ? user.getProfilePhoto() : sqlUser.getProfilePhoto());
            frontTokenComponent.refreshToken(loginUser);
        }
        return toAjax(rows);
    }

    /**
     * @return
     */
    @AuthCheck
    @GetMapping("/getInfo")
    public AjaxResult getInfo() {
        Long userId = frontTokenComponent.getUserIdNotNull();
        UserConsumer userConsumer = userConsumerService.selectUserConsumerByUserId(userId);
        return success(userConsumer);
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public AjaxResult logout(HttpServletRequest request) {
        frontTokenComponent.logout(request);
        return AjaxResult.success();
    }

}
